<?

//Initiate Authenticate

if (!GetParam("bypasslogin","")) {
    SecurityRedirect ();
}
//initiate Vars 
define("PRIMARY_KEY","group_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_tools_dumpdata.php";
$FileTemplate  = "ais_tools_dumpdata_list.html";
$TableName     = "tblgroup";
$SubHeaderTitle= "Database Dumper";
$HTMLFormName  = "DBDumper";
$PageSize      = 10 ;
$HTMLGridList  = "GroupGrid";
//************ BACKUP VARIABLES ***********


//************ FORM ACTION **************
 if (GetParam("Action","")=="Excel") {
	$FromDate = GetParam("fromDate","");
	$ToDate   = GetParam("toDate","");
	list($FDay,$FMonth,$FYear) = explode("/",$FromDate);
	list($TDay,$TMonth,$TYear) = explode("/",$ToDate);

 	 if (GetParam("dbpick","")=="TD") {
		$SQL = "SELECT * FROM ais_memorial WHERE tglbkt >= '$FYear-$FMonth-$FDay' AND tglbkt <= '$TYear-$TMonth-$TDay'";
		$FileName = "ais_memorial.txt";
	 } elseif (GetParam("dbpick","")=="AV") {
		$SQL = "SELECT * FROM ais_advance WHERE tglbkt >= '$FYear-$FMonth-$FDay' AND tglbkt <= '$TYear-$TMonth-$TDay'";
		$FileName = "ais_advance.txt";
	 } elseif (GetParam("dbpick","")=="AR") {
		$SQL = "SELECT * FROM ais_piutang WHERE tglbkt >= '$FYear-$FMonth-$FDay' AND tglbkt <= '$TYear-$TMonth-$TDay'";
		$FileName = "ais_piutang.txt";
	 } elseif (GetParam("dbpick","")=="AP") {
		$SQL = "SELECT * FROM ais_hutang WHERE tglbkt >= '$FYear-$FMonth-$FDay' AND tglbkt <= '$TYear-$TMonth-$TDay'";
		$FileName = "ais_piutang.txt";	 	
	 } elseif (GetParam("dbpick","")=="RC") {
		$SQL = "SELECT * FROM ais_receipt WHERE tglkwit >= '$FYear-$FMonth-$FDay' AND tglkwit <= '$TYear-$TMonth-$TDay'";
		$FileName = "ais_receipt.txt";	 	
	 }
 
	 DBDumper ($SQL,$FileName);		
	 die();
 }

//Initiate Database Table For Search;
$backuptype = GetParam("backuptype","");
$fromDate = GetParam("fromDate","");
$toDate   = GetParam("toDate","");
$periodDay = GetParam("periodDay","");
//******* DATABASE FORM QUERY ***********

$SQL = "SELECT * FROM `$TableName` ";
	
$SQLCount = "SELECT count(*) FROM `$TableName` ";

//Retrieve Parameter from search
if ($FCode) {
	$CustomWhere = " user like '".$FCode."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}

if ($FName) {
	$CustomWhere = " $TableName.group_id = '".$FName."'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;
}

//Build  WHERE 
if (strlen($WHERE))	$WHERE = " WHERE ".$WHERE;
else $WHERE = " WHERE 1";

$SQL = $SQL.$WHERE;
//print $SQL;
//Select User Group

//******* SHOW FORM CONTENT *********
$PageNum = 1;

$RecCnt = count($FileList);

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);


$TBS->Show();

function DBDumper ($SQL,$filename) {
	global $DBConnection;
	//print $SQL;
	$MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
	if ($MyResult) {
		foreach ($MyResult as $Row) {
			foreach ($Row as $key => $value) {
				if (!ereg("\n",$StringKey,$RegsFound)) {
					if (strlen($StringKey))	$StringKey .= "\t\"$key\"";
					else $StringKey .= "$key";
				}
				if (strlen($SVal)) $SVal .= "\t\"$value\"";
				else $SVal = "\"$value\"";
			}
			if (!ereg("\n",$StringKey,$RegsFound)) $StringKey .= "\n";
			$StringVal .= $SVal."\n";
			$SVal = "";
		}
	}
	header("Content-type:application/vnd.notepad");
	header("Content-Disposition:filename=$filename");
	$StringKey = strtoupper($StringKey);
	print $StringKey.$StringVal;
}

?>
